package com.eqdd.yiqidian.kotlin.utils

import chihane.jdaddressselector.BottomDialog
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.bean.AreaBean
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.JsonCallBack
import com.eqdd.yiqidian.utils.MultiChooseUtil
import com.lzy.okgo.OkGo
import com.lzy.okgo.cache.CacheMode
import com.lzy.okgo.model.Response

/**
 * Created by jhj on 18-9-29.
 */
object AreaUtil {
    private var areaDialog: BottomDialog? = null
    fun initAreaDialog(activity: BaseActivity, body: (province: String,city: String,district: String) -> Unit) {
        if (areaDialog == null) {
            OkGo.post<List<AreaBean>>(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS)
                    .cacheKey(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS + "0")
                    .cacheMode(CacheMode.IF_NONE_CACHE_REQUEST)
                    .params("type", 0)
                    .execute(object : JsonCallBack<List<AreaBean>>() {
                        override fun onSuccess(response: Response<List<AreaBean>>?) {
                            println("使用了网络请求")
                            val areaBean = response?.body()
                            areaDialog = MultiChooseUtil().getAreaDialog(activity, 3, areaBean) { d, a ->
                                areaDialog?.dismiss()
                                body(a[0].name,a[1].name,a[2].name)
                            }
                            areaDialog?.show()
                        }

                        override fun onCacheSuccess(response: Response<List<AreaBean>>?) {
                            println("使用了缓存")
                            val areaBean = response?.body()
                            areaDialog = MultiChooseUtil().getAreaDialog(activity, 3, areaBean) { d, a ->
                                areaDialog?.dismiss()
                                body(a[0].name,a[1].name,a[2].name)
                            }
                            areaDialog?.show()
                        }
                    })
        } else {
            areaDialog?.show()
        }
    }

}